package xml.xstream;

/**
 * <pre>
 *   Title: XStream0.java
 *   Description: 
 *   Copyright: yundaex.com Copyright (c) 2013
 *   Company: 上海韵达货运有限公司
 * </pre>
 * 
 * @author liuzhaoqing
 * @version 1.0
 * @date 2014年10月23日
 */
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;

@XStreamAlias("message")  
class Message {
	@XStreamAlias("msgType")
	private int messageType;
	
	//将true/false改为yes/no  
	@XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"yes", "no"})  
	private boolean important = false;
	
	@XStreamConverter(SingleValueCalendarConverter.class)
	private Calendar created = new GregorianCalendar();
	//对隐式集合的注解，将每一个子节点的节点名都改为part  
	@XStreamImplicit(itemFieldName="part")
	private List<String> content = null;
	
	public Message(int messageType, boolean important, String... content) {
		this.messageType = messageType;
		this.important = important;
		this.content = Arrays.asList(content);
	}
}




public class XStream2 {
   public static void main(String[] args) {
        XStream xstream = new XStream();
        //通知XStream对象读取并识别RendezvousMessage中的注解  
        xstream.processAnnotations(Message.class);  
        System.out.println(xstream.toXML(new Message(55, true, "aa","bb")));
   }
 
}
